// 维护actionCreator
import { IN_CREMENT, DE_CREMENT } from './actionTypes'
import { LOGIN_START,LOGIN_SUCCESS,LOGIN_FAIL } from './actionTypes'
import {userLogin} from '../api/user'
import {message} from 'antd'
export const increment = (payload)=>{  //数字加
  return {
    type: IN_CREMENT,
    payload
  }
}

export const decrement = (payload) => { //数字减
  return {
    type: DE_CREMENT,
    payload
  }
}


export const loginStart = (payload) => { //开始登录
  return {
    type: LOGIN_START,
    payload
  }
}

export const loginSuccess = (payload) => { //登录成功
  return {
    type: LOGIN_SUCCESS,
    payload
  }
}

export const loginFail = (payload) => { //登录失败
  return {
    type: LOGIN_FAIL,
    payload
  }
}

export const loginAsync = (payload,history)=>{ //异步action
  return dispatch => {  //【1】redux-thunk帮我们在这个地方，将dispatch做了拦截
    dispatch(loginStart()) //【2】手动触发dispatch，告知状态机开启登录
    userLogin(payload).then(res=>{ //【3】发起登录异步请求
      console.log(res);
      dispatch(loginSuccess(res.data))  // 【4-1】登录成功
      message.success('登录成功')
      history.push('/admin') //触发路由切换
    }).catch(err=>{
      console.log('登录失败',err);  
      dispatch(loginFail())  // 【4-2】登录失败
      message.error('用户名或密码错误')
    })
  }
}